package com.zxm.ssm.controller;


import com.github.pagehelper.PageInfo;
import com.zxm.ssm.entity.Inform;
import com.zxm.ssm.service.InformService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;
import java.util.Date;

@Controller
@RequestMapping("/inform")
public class InformController {

    @Autowired
    private InformService informService;

    @RequestMapping("/list")
    public String list(Model model,
                       @RequestParam(required = false,defaultValue = "1") int pageNum,
                       @RequestParam(required = false,defaultValue = "3") int pageSize){
        PageInfo<Inform> pageInfo = informService.selectInformAll(pageNum,pageSize);

        model.addAttribute("pageInfo",pageInfo);
        return "inform/list";
    }

    @RequestMapping("/del")
    public String deleteInformById(Model modle,int id){
        int ret=informService.deleteInformById(id);
        modle.addAttribute("ret",ret);
        return "redirect:/inform/list";
    }

    @GetMapping("/add")
    public String addInform(Inform inform){
        return "inform/add";
    }
    @PostMapping("/add")
    public String doAdd(Inform inform) throws IOException {
        inform.setCreateTime(new Date());
        int ret = informService.addInform(inform);
        if (ret > 0){
            /*使用绝对路径*/
            return "redirect:/inform/list";
        }
        return "inform/add";
    }
    @GetMapping("edit")
    public String toEdit(Model model, Inform inform, int id){
        inform = informService.findInformById(id);
        model.addAttribute("inform",inform);
        return "inform/edit";
    }
    @PostMapping("edit")
    public String doEdit(Inform inform) throws IOException {

        int ret = informService.updateInform(inform);
        if (ret > 0){
            /*使用绝对路径*/
            return "redirect:/inform/list";
        }
        return "inform/edit";
    }
}
